package com.solocredit.finance.ui.user.act

import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.solocredit.finance.R
import com.solocredit.finance.base.adapter.MullaOnItemChildClickListener
import com.solocredit.finance.base.utils.TitleBarUtil
import com.solocredit.finance.config.MullaConfigs
import com.solocredit.finance.databinding.ActMullaUserPointsBinding
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.mvp.MvpAct
import com.solocredit.finance.ui.user.adapter.MullaUserPointsAdapter
import com.solocredit.finance.ui.user.impl.MullaUserPointsPresenterImpl
import com.solocredit.finance.ui.user.view.IMullaIUserPointsView
import com.solocredit.finance.utils.event.MullaEventData
import com.solocredit.finance.utils.event.MullaEventType
import com.solocredit.finance.utils.helper.MullaCustomConfigsHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/12 16:07
 * @Annotation：用户积分列表
 */
class MullaUserPointsAct: MvpAct<IMullaIUserPointsView, MullaUserPointsPresenterImpl>(),IMullaIUserPointsView {
    private var _binding: ActMullaUserPointsBinding? = null
    private val binding get() = _binding ?: throw IllegalStateException(
        "Cannot access binding because it is null. Either the view hasn't been created yet, or it has already been destroyed."
    )

    var page = 1

    private var pointsAdapter: MullaUserPointsAdapter? = null
    override fun createPresenter(): MullaUserPointsPresenterImpl {
        return MullaUserPointsPresenterImpl(this)
    }

    override fun getMullaRootView(): View {
        _binding = ActMullaUserPointsBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initMullaView() {
        TitleBarUtil.setStatusBarLightMode(this, false)
        setMullaTitleBarVisibility(View.GONE)
        _binding?.contentLayout?.setPadding(0, TitleBarUtil.getStatusBarHeight(),0,0)
        initMullaRefreshLayout()
        initMullaRecyclerView()

        _binding?.ivLanguage?.visibility = View.GONE
        if (MullaConfigs.isTanz()){
            _binding?.ivLanguage?.visibility = View.VISIBLE
        }

        _binding?.textPoints?.text = getMullaUserPoints().toString()

        //牌照信息
        val licenseInfo = MullaCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo == null){
            _binding?.licenseLayout?.removeAllViews()
            _binding?.licenseLayout?.visibility = View.GONE
        } else {
            _binding?.licenseLayout?.removeAllViews()
            _binding?.licenseLayout?.addView(licenseView())
            _binding?.licenseLayout?.visibility = View.VISIBLE
        }
    }

    override fun initMullaListener() {
        _binding?.imgBack?.setOnClickListener {
            finish()
        }
        //客服
        _binding?.imgKefu?.setOnClickListener {
            showMullaKefuDialog("Points Details Page")
            HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_CUSTOMER,"Points Details Page")
        }

        //语言切换
        _binding?.ivLanguage?.setOnClickListener {
            HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_LANGUAGE,"Points Details Page")
            showMullaLanguageDialog("PointsList Page", onSelectCallback = {
            })
        }
    }

    override fun initMullaData() {
        showMullaLoading(isTOuchOut = false, isCancel = true)
        onMullaRefreshAction()
    }


    /* 初始化刷新 */
    private fun initMullaRefreshLayout() {
        //禁止加载
        _binding?.refreshLayout?.setEnableLoadMore(true)
        _binding?.refreshLayout?.setEnableAutoLoadMore(false)
        _binding?.refreshLayout?.setOnRefreshListener {
            onMullaRefreshAction()
        }
        _binding?.refreshLayout?.setOnLoadMoreListener {
            onMullaLoadMoreAction()
        }
    }

    /**
     * 初始化积分明细列表
     */
    private fun initMullaRecyclerView() {
//        _binding?.recyclerView.setHasFixedSize(true)
//        _binding?.recyclerView.isNestedScrollingEnabled = false
//        _binding?.recyclerView.isFocusableInTouchMode = false //不需要焦点
//        val topDecoration = DensityUtils.dp2px(10f)
//        _binding?.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0,0,topDecoration,0))
        _binding?.recyclerView?.layoutManager = LinearLayoutManager(this)
        pointsAdapter = MullaUserPointsAdapter(binding.recyclerView)
        _binding?.recyclerView?.adapter = pointsAdapter
        _binding?.recyclerView?.itemAnimator = DefaultItemAnimator()
        pointsAdapter?.setOnItemChildClickListener(MullaOnItemChildClickListener { _, childView, position ->
            val item = pointsAdapter?.getItem(position)
            when (childView.id) {
                R.id.itemLayout -> {//点击Item

                }
            }
        })
    }

    /**
     * 刷新
     */
    private fun onMullaRefreshAction(){
        page = 1
        presenter.loadMullaUserPointsListAction()
    }

    /**
     * 加载更多
     */
    private fun onMullaLoadMoreAction(){
        page++
        presenter.loadMullaUserPointsListAction()
    }

    override fun loadMullaPage(): Int {
        return page
    }

    /**
     * 设置积分明细
     */
    override fun showMullaUserPointsList(totalCount: Int, pointsList: ArrayList<JSONObject>) {
        if (page == 1){
            pointsAdapter?.data = pointsList
        } else {
            pointsAdapter?.addMoreData(pointsList)
        }
        pointsAdapter?.let {
            if (it.itemCount >= totalCount){
                _binding?.refreshLayout?.finishLoadMoreWithNoMoreData()
            } else {
                _binding?.refreshLayout?.resetNoMoreData()
            }
        }
    }

    override fun hideMullaLoading() {
        super.hideMullaLoading()
        _binding?.refreshLayout?.finishRefresh()
        _binding?.refreshLayout?.finishLoadMore()
    }

    override fun onMullaEvent(event: MullaEventData) {
        //刷新积分完成
        if (event.eventType == MullaEventData.TYPE.MULLA_POINTS_REFRESHED){
            _binding?.textPoints?.text = getMullaUserPoints().toString()
        }
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addMullaEventAction(MullaEventType.PAGE.MULLA_PAGE_POINTS_LIST_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        _binding = null
        HttpHelper.addMullaEventAction(MullaEventType.PAGE.MULLA_PAGE_POINTS_LIST_END)
    }
}